package com.zhou.base.gateway.sample;

import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

public class MyLoadBalancerClientsConfiguration {

    private ServiceInstanceListSupplier baseServiceInstanceListSupplier(
            ConfigurableApplicationContext context) {
        return ServiceInstanceListSupplier.builder().withDiscoveryClient().withCaching()
                .build(context);
    }

    @Bean
    public ServiceInstanceListSupplier myZonePreferenceServiceInstanceListSupplier(ConfigurableApplicationContext context) {
        LoadBalancerZoneConfig zoneConfig = context.getBean(LoadBalancerZoneConfig.class);
        return new MyZonePreferenceServiceInstanceListSupplier(baseServiceInstanceListSupplier(context), zoneConfig);
    }
}
